<template>
  <div class="page-loading" ref="pageLoadingPanel" v-if="!hidden">
    <image :src="loadingIcon" class="loading-img"></image>
  </div>
</template>
<style lang="scss">
  @import "../sass/var";
  @import "../sass/mixin";

  .page-loading {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    justify-content: center;
    align-items: center;
    background-color: $body-bg;
  }

  .text-tip {
    color: $color-muted;
    font-weight: lighter;
    font-size: 28px;
  }

  .loading-img {
    width: 100px;
    height: 100px;
  }

  .loading-icon {
    width: 60px;
    height: 60px;
    background-color: #dfdfdf;
    border-radius: 5px;
    @include border(1px, solid, #d6d6d6)
  }
</style>
<script>

const animation = weex.requireModule('animation')
let urlPath = require('../include/url-path')
let styles = [
  {
    transform: 'perspective(120px) rotateX(0deg) rotateY(0deg)'
  },
  {
    transform: 'perspective(120px) rotateX(-180.1deg) rotateY(0deg)'
  },
  {
    transform: 'perspective(120px) rotateX(-180deg) rotateY(-179.9deg)'
  }
]
module.exports = {
  props: {
    loaded: {default: false}
  },

  data () {
    return {
      hidden: false,
      loadingIcon: urlPath.assets('images/loading.gif')
    }
  },
  components: {},
  created () {
    this.$watch('loaded', (newVal, oldVal) => {
      newVal && this.hide()
    })
  },
  mounted () {
    this.step(0, 0)
  },
  methods: {
    hide () {
      animation.transition(this.$refs.pageLoadingPanel, {
        styles: {
          opacity: 0
        },
        duration: 200, // ms
        delay: 0 // ms
      }, () => {
        this.hidden = true
      })
    },
    step (idx, duration) {
      if (idx >= styles.length) {
        idx = 0
        duration = 0
      }
      if (this.loaded) {
        this.hide()
        return
      }
      animation.transition(this.$refs.loadingIcon, {
        styles: styles[idx],
        duration: duration, // ms
        delay: 0 // ms
      }, () => {
        this.step(idx + 1, 600)
      })
    }
  }
}
</script>
